Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
fill-range
Advanced tools
Fill in a range of numbers or letters, optionally passing an increment or `step` to use, or create a regex-compatible range with `options.toRegex`
The fill-range npm package is used to generate an array of numbers or strings based on a given range with support for steps, negative ranges, and pattern expansion. It is often used for creating lists of numbers, expanding number and letter sequences, and generating arrays based on patterns.
Number Range Generation
Generates an array of numbers from 1 to 5.
[...fillRange(1, 5)]
Letter Range Generation
Generates an array of letters from 'a' to 'e'.
[...fillRange('a', 'e')]
Pattern Expansion
Expands a pattern '1..3' and applies a transformation function to each value, resulting in ['item1', 'item2', 'item3'].
[...fillRange('1..3', { transform: (val) => `item${val}` })]
Step Support
Generates an array of numbers from 1 to 10 with a step of 2, resulting in [1, 3, 5, 7, 9].
[...fillRange(1, 10, { step: 2 })]
Negative Range Support
Generates an array of numbers from -3 to 3.
[...fillRange(-3, 3)]
This package provides similar functionality to fill-range by generating an array of numbers within a specified range. It allows for custom steps and can handle both ascending and descending ranges. Compared to fill-range, it focuses specifically on number ranges and does not support pattern expansion or letter ranges.
Part of the Lodash utility library, lodash.range generates an array of numbers progressing from start up to, but not including, end. It supports steps and is a lightweight alternative for number range generation. However, it does not offer the pattern expansion or letter range capabilities that fill-range does.
Fill in a range of numbers or letters, optionally passing an increment or
step
to use, or create a regex-compatible range withoptions.toRegex
(TOC generated by verb using markdown-toc)
Install with npm:
$ npm install --save fill-range
Install with yarn:
$ yarn add fill-range
Expands numbers and letters, optionally using a step
as the last argument. (Numbers may be defined as JavaScript numbers or strings).
var fill = require('fill-range');
fill(from, to[, step, options]);
// examples
console.log(fill('1', '10')); //=> '[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ]'
console.log(fill('1', '10', {toRegex: true})); //=> [1-9]|10
Params
from
: {String|Number} the number or letter to start withto
: {String|Number} the number or letter to end withstep
: {String|Number|Object|Function} Optionally pass a step to use.options
: {Object|Function}: See all available optionsBy default, an array of values is returned.
Alphabetical ranges
console.log(fill('a', 'e')); //=> ['a', 'b', 'c', 'd', 'e']
console.log(fill('A', 'E')); //=> [ 'A', 'B', 'C', 'D', 'E' ]
Numerical ranges
Numbers can be defined as actual numbers or strings.
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
console.log(fill('1', '5')); //=> [ 1, 2, 3, 4, 5 ]
Negative ranges
Numbers can be defined as actual numbers or strings.
console.log(fill('-5', '-1')); //=> [ '-5', '-4', '-3', '-2', '-1' ]
console.log(fill('-5', '5')); //=> [ '-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5' ]
Steps (increments)
// numerical ranges with increments
console.log(fill('0', '25', 4)); //=> [ '0', '4', '8', '12', '16', '20', '24' ]
console.log(fill('0', '25', 5)); //=> [ '0', '5', '10', '15', '20', '25' ]
console.log(fill('0', '25', 6)); //=> [ '0', '6', '12', '18', '24' ]
// alphabetical ranges with increments
console.log(fill('a', 'z', 4)); //=> [ 'a', 'e', 'i', 'm', 'q', 'u', 'y' ]
console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
console.log(fill('a', 'z', 6)); //=> [ 'a', 'g', 'm', 's', 'y' ]
Type: number
(formatted as a string or number)
Default: undefined
Description: The increment to use for the range. Can be used with letters or numbers.
Example(s)
// numbers
console.log(fill('1', '10', 2)); //=> [ '1', '3', '5', '7', '9' ]
console.log(fill('1', '10', 3)); //=> [ '1', '4', '7', '10' ]
console.log(fill('1', '10', 4)); //=> [ '1', '5', '9' ]
// letters
console.log(fill('a', 'z', 5)); //=> [ 'a', 'f', 'k', 'p', 'u', 'z' ]
console.log(fill('a', 'z', 7)); //=> [ 'a', 'h', 'o', 'v' ]
console.log(fill('a', 'z', 9)); //=> [ 'a', 'j', 's' ]
Type: boolean
Default: false
Description: By default, null
is returned when an invalid range is passed. Enable this option to throw a RangeError
on invalid ranges.
Example(s)
The following are all invalid:
fill('1.1', '2'); // decimals not supported in ranges
fill('a', '2'); // incompatible range values
fill(1, 10, 'foo'); // invalid "step" argument
Type: boolean
Default: undefined
Description: Cast all returned values to strings. By default, integers are returned as numbers.
Example(s)
console.log(fill(1, 5)); //=> [ 1, 2, 3, 4, 5 ]
console.log(fill(1, 5, {stringify: true})); //=> [ '1', '2', '3', '4', '5' ]
Type: boolean
Default: undefined
Description: Create a regex-compatible source string, instead of expanding values to an array.
Example(s)
// alphabetical range
console.log(fill('a', 'e', {toRegex: true})); //=> '[a-e]'
// alphabetical with step
console.log(fill('a', 'z', 3, {toRegex: true})); //=> 'a|d|g|j|m|p|s|v|y'
// numerical range
console.log(fill('1', '100', {toRegex: true})); //=> '[1-9]|[1-9][0-9]|100'
// numerical range with zero padding
console.log(fill('000001', '100000', {toRegex: true}));
//=> '0{5}[1-9]|0{4}[1-9][0-9]|0{3}[1-9][0-9]{2}|0{2}[1-9][0-9]{3}|0[1-9][0-9]{4}|100000'
Type: function
Default: undefined
Description: Customize each value in the returned array (or string). (you can also pass this function as the last argument to fill()
).
Example(s)
// increase padding by two
var arr = fill('01', '05', function(val, a, b, step, idx, arr, options) {
return repeat('0', (options.maxLength + 2) - val.length) + val;
});
console.log(arr);
//=> ['0001', '0002', '0003', '0004', '0005']
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Commits | Contributor |
---|---|
103 | jonschlinkert |
2 | paulmillr |
1 | edorivai |
1 | wtgtybhertgeghgtwtg |
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.5.0, on April 23, 2017.
FAQs
Fill in a range of numbers or letters, optionally passing an increment or `step` to use, or create a regex-compatible range with `options.toRegex`
The npm package fill-range receives a total of 64,267,334 weekly downloads. As such, fill-range popularity was classified as popular.
We found that fill-range demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.